/**
 * @file	Layout.h
 * @brief	This is a class which uses to manager elements in a layout.
 * @author	Shaokp
 * @date	2011-11-24
 * @version	0.1
 */

#ifndef	LAYOUT_H
#define LAYOUT_H
#include <map>
#include <string>
#include "Element.h"

struct LayoutSettings
{
	int mModuleNumX;
	int mModuleNumY;
	double mModuleSize;
	double mModuleSpace;
	double mModuleBufferExt;
	double mConnectionWidth;
	double mConnectionSpace;
	double mConnectionModuleSpace;
	double mCellBufferExt;
};

class PathGridNode
{
public:
	PathGridNode(Point pos);

	void setCoordinate(Point pos);
	Point getCoordinate() const;
	void setOccupiedFlag(bool flag);
	bool isOccupied() const;
	void setTargetFlag(bool flag);
	bool isTarget() const;
	void setSourceFlag(bool flag);
	bool isSource() const;
	void setWeight(unsigned int weight);
	unsigned int getWeight() const;
private:
	Point mCoordinate;
	bool mIsOccupied;
	bool mIsTarget;
	bool mIsSource;
	unsigned int mWeight;
	int mTraceCode;
};

class Layout
{
public:
	Layout(LayoutSettings settings);
	~Layout();

	void addElement(Element *element);
	void delElement(Element *element);
	Element *getElement(std::string &objName) const;
	/**
	 * @brief	Create grid of layout.
	 * @param grid	Grid created.
	 */
	void createGrid(std::vector<std::vector<PathGridNode> > &grid);
	/**
	 * @brief	Add a point into current grid.
	 *			The grid may add a column or a row according the relation between coordinate.
	 * @param grid	Grid will be changed.
	 * @param point Added point.
	 */
	void addOnePointIntoGrid(std::vector<std::vector<PathGridNode> > &grid, Point point, bool isOccupied);
private:
	/**
	 * @brief	Add a element into current grid.
	 *			It will add ends of element into current grid.
	 * @param grid	Grid will be changed.
	 * @param point Added point.
	 */
	void addOneElementIntoGrid(std::vector<std::vector<PathGridNode> > &grid, Element *element);
	
	std::map<std::string, Element *> mElements;
	LayoutSettings mSettings;
};
#endif
